package com.whiteape.book;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.whiteape.book.common.WaConstants;
import com.whiteape.book.model.dto.SiteSourceBean;
import com.whiteape.book.model.dto.SiteSourceContentInfoBean;
import com.whiteape.book.model.dto.SiteSourceMediaInfoBean;
import com.whiteape.book.model.dto.SiteSourceChapterInfoBean;
import com.whiteape.book.model.dto.SiteSourceSearchBean;
import com.whiteape.book.model.dto.SiteSourceSearchResultBean;
import com.whiteape.book.model.gen.SiteSourceBeanDao;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.utils.DaoDbHelper;
import com.whiteape.book.utils.FileUtils;
import com.whiteape.book.utils.GsonUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        importSource();
//        printSource();

    }

    private static void importAiQiWenXue() {
        SiteSourceBean siteSourceBean = new SiteSourceBean();
        siteSourceBean.setCreator("突突突突");
        siteSourceBean.setSiteHostUrl("https://www.i7wx.com");
        siteSourceBean.setSiteIconUrl("https://www.i7wx.com/skin/images/logo.png");
        siteSourceBean.setSiteName("爱奇文学");
        siteSourceBean.setSiteDesc("速度快;内容全");
        siteSourceBean.setSiteType(MediaType.MEDIA_TYPE_NOVEL);

        SiteSourceSearchBean siteSourceSearchBean = new SiteSourceSearchBean();
        SiteSourceSearchResultBean siteSourceSearchResultBean = new SiteSourceSearchResultBean();
        siteSourceSearchResultBean.setList("//*[@id=\"nr\"]");
        siteSourceSearchResultBean.setDetailUrl("//td[1]/a/@href");
//        siteSourceSearchResultBean.setCoverUrl("");
        siteSourceSearchResultBean.setAuthor("//td[3]/text()");
        siteSourceSearchResultBean.setTitle("//td[1]/a/text()");
        siteSourceSearchResultBean.setLastChapter("//td[2]/a/text()");
        siteSourceSearchResultBean.setLastUpdated("//td[5]/text()");
        siteSourceSearchResultBean.setState("//td[6]/text()");
        siteSourceSearchBean.setResult(siteSourceSearchResultBean);
        siteSourceSearchBean.setUrl("/modules/article/search.php");
        siteSourceSearchBean.setCharset("gbk");
        Map<String, String> params = new HashMap<>();
        params.put("searchkey", "searchKey");
        params.put("searchtype", "articlename");
        siteSourceSearchBean.setParams(params);
//        siteSourceSearchBean.setHeaders(Collections.emptyMap());
        siteSourceBean.setSearch(siteSourceSearchBean);

        SiteSourceMediaInfoBean bookInfoBean = new SiteSourceMediaInfoBean();
        bookInfoBean.setTitle("//div[@class=\"booktitle\"]/h1/text()");
        bookInfoBean.setAuthor("//span[@id=\"author\"]/a/text()");
        bookInfoBean.setLastChapter("//span[@class=\"new_1\"]/a/@href");
        bookInfoBean.setCoverUrl("//div[@id=\"bookimg\"]/img/@src");
        bookInfoBean.setCatalogUrl("//*[@id=\"bookinfo\"]/div[3]/a[1]/@href");
        bookInfoBean.setDesc("//*[@id=\"bookintro\"]/p/text()");
        siteSourceBean.setMediaInfoDetailRule(bookInfoBean);

        SiteSourceChapterInfoBean chapterInfoBean = new SiteSourceChapterInfoBean();
        chapterInfoBean.setList("//*[@id=\"readerlist\"]/ul/li[position() > 1]");
        chapterInfoBean.setUrl("//a/@href");
        chapterInfoBean.setName("//a/text()");
        chapterInfoBean.setOrderAsc(true);
        siteSourceBean.setChapterInfoRule(chapterInfoBean);

        SiteSourceContentInfoBean contentInfoBean = new SiteSourceContentInfoBean();
        contentInfoBean.setContent("//*[@id=\"content\"]/html()");
        siteSourceBean.setContentInfoRule(contentInfoBean);





//        SiteSourceManager.getInstance().saveBookSource(siteSourceBean);
//        System.out.println("NEW GET is: " + SiteSourceManager.getInstance().getBookSource("https://www.qishu.tw"));
//        siteSourceBean.setBookSourceBookSortBean(null);
//        siteSourceBean.setBookSourceChapterInfoBean(null);
//        siteSourceBean.setBookSourceSearchBean(null);
        DaoDbHelper.getInstance().getSession().insertOrReplace(siteSourceBean);
    }

    private static void importZhuiShuShenQi() {
        SiteSourceBean siteSourceBean = new SiteSourceBean();
        siteSourceBean.setCreator("村头王师傅");
        siteSourceBean.setSiteHostUrl("http://api.zhuishushenqi.com/");
//        siteSourceBean.setSiteIconUrl("https://www.i7wx.com/skin/images/logo.png");
        siteSourceBean.setSiteName("追书神器Api");
        siteSourceBean.setSiteDesc("速度快;内容全");
        siteSourceBean.setSiteType(MediaType.MEDIA_TYPE_NOVEL);

        SiteSourceSearchBean siteSourceSearchBean = new SiteSourceSearchBean();
        SiteSourceSearchResultBean siteSourceSearchResultBean = new SiteSourceSearchResultBean();
        siteSourceSearchResultBean.setList("$..books[*]");
        siteSourceSearchResultBean.setDetailUrl("@JSon:$._id|||@JS:'http://api.zhuishushenqi.com/atoc?view=summary&book=' + $content;");
        siteSourceSearchResultBean.setCoverUrl("@JSon:$.cover|||@JS:'http://statics.zhuishushenqi.com' + $content;");
        siteSourceSearchResultBean.setAuthor("$.author{{author:=$.author}}");
        siteSourceSearchResultBean.setTitle("$.title");
        siteSourceSearchResultBean.setLastChapter("$.lastChapter");
//        siteSourceSearchResultBean.setLastUpdated("//td[5]/text()");
//        siteSourceSearchResultBean.setState("//td[6]/text()");
        siteSourceSearchBean.setResult(siteSourceSearchResultBean);
        siteSourceSearchBean.setUrl("/book/fuzzy-search");
        siteSourceSearchBean.setCharset("utf-8");
        Map<String, String> params = new HashMap<>();
        params.put("query", "searchKey");
        siteSourceSearchBean.setParams(params);
//        siteSourceSearchBean.setHeaders(Collections.emptyMap());
        siteSourceBean.setSearch(siteSourceSearchBean);

        SiteSourceMediaInfoBean bookInfoBean = new SiteSourceMediaInfoBean();
        bookInfoBean.setTitle("");
        bookInfoBean.setAuthor("@JS:'{{author}}'");
        bookInfoBean.setLastChapter("");
        bookInfoBean.setCoverUrl("");
        bookInfoBean.setCatalogUrl("@JSon:$.._id|||@JS:$c = JSON.parse($content);'http://api.zhuishushenqi.com/atoc/' + $c[0] + '?view=chapters'");
        bookInfoBean.setDesc("");
        siteSourceBean.setMediaInfoDetailRule(bookInfoBean);

        SiteSourceChapterInfoBean chapterInfoBean = new SiteSourceChapterInfoBean();
        chapterInfoBean.setList("@JSon:$.chapters");
        chapterInfoBean.setUrl("@JSon:$.link|||@JS:'http://chapterup.zhuishushenqi.com/chapter/' + encodeURIComponent($content)");
        chapterInfoBean.setName("$.title");
        chapterInfoBean.setOrderAsc(true);
        siteSourceBean.setChapterInfoRule(chapterInfoBean);

        SiteSourceContentInfoBean contentInfoBean = new SiteSourceContentInfoBean();
        contentInfoBean.setContent("@JSon:$..body");
        siteSourceBean.setContentInfoRule(contentInfoBean);




//        SiteSourceManager.getInstance().saveBookSource(siteSourceBean);
//        System.out.println("NEW GET is: " + SiteSourceManager.getInstance().getBookSource("https://www.qishu.tw"));
//        siteSourceBean.setBookSourceBookSortBean(null);
//        siteSourceBean.setBookSourceChapterInfoBean(null);
//        siteSourceBean.setBookSourceSearchBean(null);
        DaoDbHelper.getInstance().getSession().insertOrReplace(siteSourceBean);
    }

    private static void importXiaoMiShuCheng() {
        SiteSourceBean siteSourceBean = new SiteSourceBean();
        siteSourceBean.setCreator("村头王师傅");
        siteSourceBean.setSiteHostUrl("https://reader.browser.miui.com");
//        siteSourceBean.setSiteIconUrl("https://s02.mifile.cn/assets/static/image/logo-footer.png");
        siteSourceBean.setSiteName("小米书城");
        siteSourceBean.setSiteDesc("速度快;内容全");
        siteSourceBean.setSiteType(MediaType.MEDIA_TYPE_NOVEL);

        SiteSourceSearchBean siteSourceSearchBean = new SiteSourceSearchBean();
        SiteSourceSearchResultBean siteSourceSearchResultBean = new SiteSourceSearchResultBean();
        siteSourceSearchResultBean.setList("$.related");
        siteSourceSearchResultBean.setDetailUrl("@JSon:$.id|||@JS:'/api/v2/book/' + $content;");
        siteSourceSearchResultBean.setCoverUrl("@JSon:$.image");
        siteSourceSearchResultBean.setAuthor("$.author");
        siteSourceSearchResultBean.setTitle("$.title");
        siteSourceSearchResultBean.setDesc("$.description|||@JS:var $ret = $content.trim()");
        siteSourceSearchResultBean.setLastChapter("");
//        siteSourceSearchResultBean.setLastUpdated("//td[5]/text()");
//        siteSourceSearchResultBean.setState("//td[6]/text()");
        siteSourceSearchBean.setResult(siteSourceSearchResultBean);
        siteSourceSearchBean.setUrl("/search-api/v2/novel/search");
        siteSourceSearchBean.setCharset("utf-8");
        Map<String, String> params = new HashMap<>();
        params.put("query", "searchKey");
        params.put("start", "0");
        params.put("size", "10");
        siteSourceSearchBean.setParams(params);
//        siteSourceSearchBean.setHeaders(Collections.emptyMap());
        siteSourceBean.setSearch(siteSourceSearchBean);

        SiteSourceMediaInfoBean bookInfoBean = new SiteSourceMediaInfoBean();
        bookInfoBean.setTitle("");
        bookInfoBean.setAuthor("");
        bookInfoBean.setLastChapter("");
        bookInfoBean.setCoverUrl("");
        bookInfoBean.setCatalogUrl("@JSON:$.data.bookInfo.bookId{{bookId:=$.data.bookInfo.bookId}} ||| @JS: '/api/v2/chapter/list/' + $content");
        bookInfoBean.setDesc("");
        siteSourceBean.setMediaInfoDetailRule(bookInfoBean);

        SiteSourceChapterInfoBean chapterInfoBean = new SiteSourceChapterInfoBean();
        chapterInfoBean.setList("@JSon:$.data.list");
        chapterInfoBean.setUrl("@STR:/api/v2/chapter/content/{{bookId}}/?chapterId={{$.chapterId}}&volumeId={{$.volumeIndex}}");
        chapterInfoBean.setName("$.chapterName");
        chapterInfoBean.setOrderAsc(true);
        siteSourceBean.setChapterInfoRule(chapterInfoBean);

        SiteSourceContentInfoBean contentInfoBean = new SiteSourceContentInfoBean();
        contentInfoBean.setContent("@JSon:$.data.contentList[*] ||| @JS: JSON.parse($content).join('\\n');");
        siteSourceBean.setContentInfoRule(contentInfoBean);




//        SiteSourceManager.getInstance().saveBookSource(siteSourceBean);
//        System.out.println("NEW GET is: " + SiteSourceManager.getInstance().getBookSource("https://www.qishu.tw"));
//        siteSourceBean.setBookSourceBookSortBean(null);
//        siteSourceBean.setBookSourceChapterInfoBean(null);
//        siteSourceBean.setBookSourceSearchBean(null);
        DaoDbHelper.getInstance().getSession().insertOrReplace(siteSourceBean);
    }


    private static void importAiKanXiaoShuo() {
        SiteSourceBean siteSourceBean = new SiteSourceBean();
        siteSourceBean.setCreator("漩涡鸣人");
        siteSourceBean.setSiteHostUrl("https://www.aikantxt.la/");
        siteSourceBean.setSiteIconUrl("https://www.aikantxt.la/images/logo.png");
        siteSourceBean.setSiteName("爱看小说网");
        siteSourceBean.setSiteDesc("速度快;内容全");
        siteSourceBean.setSiteType(MediaType.MEDIA_TYPE_NOVEL);

        SiteSourceSearchBean siteSourceSearchBean = new SiteSourceSearchBean();
        SiteSourceSearchResultBean siteSourceSearchResultBean = new SiteSourceSearchResultBean();
        siteSourceSearchResultBean.setList("#nr");
        siteSourceSearchResultBean.setDetailUrl("td:nth-child(1) > a @href");
        siteSourceSearchResultBean.setAuthor("td:nth-child(3) @text");
        siteSourceSearchResultBean.setTitle("td:nth-child(1) > a @text");
        siteSourceSearchResultBean.setLastChapter("td:nth-child(2) > a @text");
        siteSourceSearchResultBean.setLastUpdated("td:nth-child(5) @text");
        siteSourceSearchResultBean.setState("td:nth-child(6) @text");
        siteSourceSearchBean.setResult(siteSourceSearchResultBean);
        siteSourceSearchBean.setUrl("/modules/article/search.php");
        siteSourceSearchBean.setCharset("gbk");
        Map<String, String> params = new HashMap<>();
        params.put("searchkey", "searchKey");
        params.put("searchtype", "articlename");
        siteSourceSearchBean.setParams(params);
//        siteSourceSearchBean.setHeaders(Collections.emptyMap());
        siteSourceBean.setSearch(siteSourceSearchBean);

        SiteSourceMediaInfoBean bookInfoBean = new SiteSourceMediaInfoBean();
        bookInfoBean.setTitle("#info > h1 @text");
        bookInfoBean.setAuthor("#info > p:nth-child(2) > a @text");
        bookInfoBean.setLastChapter("#info > p:nth-child(5) > a @text");
        bookInfoBean.setCoverUrl("#fmimg > img @src");
        bookInfoBean.setDesc("#intro > p@text");
        siteSourceBean.setMediaInfoDetailRule(bookInfoBean);

        SiteSourceChapterInfoBean chapterInfoBean = new SiteSourceChapterInfoBean();
        chapterInfoBean.setList("#list > dl > dd");
        chapterInfoBean.setUrl("a @href");
        chapterInfoBean.setName("a @text");
        chapterInfoBean.setOrderAsc(true);
        siteSourceBean.setChapterInfoRule(chapterInfoBean);

        SiteSourceContentInfoBean contentInfoBean = new SiteSourceContentInfoBean();
        contentInfoBean.setContent("#content @html");
        siteSourceBean.setContentInfoRule(contentInfoBean);




//        SiteSourceManager.getInstance().saveBookSource(siteSourceBean);
//        System.out.println("NEW GET is: " + SiteSourceManager.getInstance().getBookSource("https://www.qishu.tw"));
//        siteSourceBean.setBookSourceBookSortBean(null);
//        siteSourceBean.setBookSourceChapterInfoBean(null);
//        siteSourceBean.setBookSourceSearchBean(null);
        DaoDbHelper.getInstance().getSession().insertOrReplace(siteSourceBean);

    }

    private static void importQiShuWang() {

        SiteSourceBean siteSourceBean = new SiteSourceBean();
        siteSourceBean.setCreator("卧虎");
        siteSourceBean.setSiteHostUrl("https://www.qishu.tw");
        siteSourceBean.setSiteIconUrl("https://www.qishu.tw/biqule/images/logo.gif");
        siteSourceBean.setSiteName("奇书网");
        siteSourceBean.setSiteDesc("比较慢;凑合用");
        siteSourceBean.setSiteType(MediaType.MEDIA_TYPE_NOVEL);

        SiteSourceSearchBean siteSourceSearchBean = new SiteSourceSearchBean();
        SiteSourceSearchResultBean siteSourceSearchResultBean = new SiteSourceSearchResultBean();
        siteSourceSearchResultBean.setDetailUrl("");
        siteSourceSearchResultBean.setList(".novelslistss > li");
        siteSourceSearchResultBean.setDetailUrl(".s2 > a:nth-of-type(1) @href");
        siteSourceSearchResultBean.setAuthor(".s4 @text");
        siteSourceSearchResultBean.setTitle(".s2 > a:nth-of-type(1) @text");
        siteSourceSearchResultBean.setLastChapter("span.s3 > a @text");
        siteSourceSearchResultBean.setLastUpdated("span.s5 > a @text");
        siteSourceSearchBean.setResult(siteSourceSearchResultBean);
        siteSourceSearchBean.setUrl("/modules/article/search.php");
        siteSourceSearchBean.setCharset("gbk");
        Map<String, String> params = new HashMap<>();
        params.put("searchkey", "searchKey");
        params.put("searchtype", "articlename");
        siteSourceSearchBean.setParams(params);
//        siteSourceSearchBean.setHeaders(Collections.emptyMap());
        siteSourceBean.setSearch(siteSourceSearchBean);

        SiteSourceChapterInfoBean chapterInfoBean = new SiteSourceChapterInfoBean();
        chapterInfoBean.setList("body > div:nth-child(4) > div > dl > dd");
        chapterInfoBean.setUrl("a @href");
        chapterInfoBean.setName("a @text");
        chapterInfoBean.setOrderAsc(true);
        siteSourceBean.setChapterInfoRule(chapterInfoBean);

        SiteSourceContentInfoBean contentInfoBean = new SiteSourceContentInfoBean();
        contentInfoBean.setContent("#content@html|||@JS:$content.substring($content.indexOf('最新章节！') +5)");
        siteSourceBean.setContentInfoRule(contentInfoBean);


        SiteSourceMediaInfoBean bookInfoBean = new SiteSourceMediaInfoBean();
        bookInfoBean.setTitle("#info > h1 @text");
        bookInfoBean.setAuthor("#info > p:nth-child(2) > a @text");
        bookInfoBean.setLastChapter("#info > p:nth-child(5) > a @text");
        bookInfoBean.setCoverUrl("#fmimg > img @src");
        bookInfoBean.setLastUpdated("#info > p:nth-child(4) @ text|||@JS:$content.slice($content.indexOf('时间：') + 3,$content.indexOf(' ')+1)");
        bookInfoBean.setWordCount("#info > p:nth-child(4) @text |||@JS:$content.slice($content.indexOf('共') + 1,$content.indexOf('万')+1)");
        bookInfoBean.setDesc("#intro @text");

        siteSourceBean.setMediaInfoDetailRule(bookInfoBean);


//        HashMap<String, String> categoryMap = new HashMap<>();
//        categoryMap.put("周点击榜", "https://www.qishu.tw/top/weekvisit_1.html");
//        categoryMap.put("月点击榜", "https://www.qishu.tw/top/monthvisit_1.html");
//        categoryMap.put("总点击榜", "https://www.qishu.tw/top/allvisit_1.html");
//        categoryMap.put("周推荐榜", "https://www.qishu.tw/top/weekvote_1.html");
//        categoryMap.put("月推荐榜", "https://www.qishu.tw/top/monthvote_1.html");
//        categoryMap.put("总推荐榜", "https://www.qishu.tw/top/allvote_1.html");
//        categoryMap.put("总收藏榜", "https://www.qishu.tw/top/goodnum_1.html");
//        categoryMap.put("总字数榜", "https://www.qishu.tw/top/size_1.html");
//        categoryMap.put("最新入库", "https://www.qishu.tw/top/postdate_1.html");
//        categoryMap.put("最近更新", "https://www.qishu.tw/top/lastupdate_1.html");
//        siteSourceBean.setMediaCategory(categoryMap);
//        SiteSourceCategoryInfoBean categoryInfoBean = new SiteSourceCategoryInfoBean();
//        categoryInfoBean.setList("#main > div > li");
//        categoryInfoBean.setNextPage("#pagelink > a.next @href");
//        categoryInfoBean.setDetailUrl("span.s2 > a@href");
//        categoryInfoBean.setCategory("span.s1 @text|||@JS:$content.replace(/[\\[\\]]/g,'')");
//        categoryInfoBean.setTitle("span.s2 > a @text");
//        categoryInfoBean.setAuthor("span.s4 @text");
//        categoryInfoBean.setLastChapter("span.s3 > a @text");
//        categoryInfoBean.setLastUpdated("span.s5 @text ||| @JS:$content.substring(0, $content.indexOf(' '))");
//        categoryInfoBean.setState("span.s7@text");
//        siteSourceBean.setMediaCategoryRule(categoryInfoBean);
//        SiteSourceManager.getInstance().saveBookSource(siteSourceBean);
//        System.out.println("NEW GET is: " + SiteSourceManager.getInstance().getBookSource("https://www.qishu.tw"));
//        siteSourceBean.setBookSourceBookSortBean(null);
//        siteSourceBean.setBookSourceChapterInfoBean(null);
//        siteSourceBean.setBookSourceSearchBean(null);
        DaoDbHelper.getInstance().getSession().insertOrReplace(siteSourceBean);
    }
    public static void importSource() {

//        importXiaoMiShuCheng();
//        importZhuiShuShenQi();
//        importAiQiWenXue();
//        importAiKanXiaoShuo();
//        importQiShuWang();

//        importGuFengManHua();
//        importTencent();
//        importChuiXue();

//        import2Animx();

        List<SiteSourceBean> novelList = DaoDbHelper.getInstance().getSession()
                .getSiteSourceBeanDao()
                .queryBuilder()
                .where(SiteSourceBeanDao.Properties.SiteType.eq(0))
                .build()
                .list();
        List<SiteSourceBean> comicList = DaoDbHelper.getInstance().getSession()
                .getSiteSourceBeanDao()
                .queryBuilder()
                .where(SiteSourceBeanDao.Properties.SiteType.eq(1))
                .build()
                .list();
//        System.out.println("请注意，请注意!!!!!!!");
//        System.out.println(GsonUtils.obj2String(list));
        File novel = new File(WaConstants.MEDIA_CACHE_PATH +  "novel.json");
        File comic = new File(WaConstants.MEDIA_CACHE_PATH +  "comic.json");
        FileUtils.saveFile(new ByteArrayInputStream(GsonUtils.obj2String(novelList).getBytes()), novel);
        FileUtils.saveFile(new ByteArrayInputStream(GsonUtils.obj2String(comicList).getBytes()), comic);






//        parse(siteSourceBean);
//        Disposable disposable = WebMediaManager.getInstance().getSearchInfo(siteSourceBean, "遮天")
//                .subscribeOn(Schedulers.newThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .flatMap(resultBeans -> WebMediaManager.getInstance().getMediaInfo(siteSourceBean, resultBeans.get(0).getDetailUrl()))
//                .flatMap(bookInfoBean1 -> WebMediaManager.getInstance().getCatalog(siteSourceBean, bookInfoBean1.getCatalogUrl()))
//                .flatMap(chapterBos -> WebMediaManager.getInstance().getBookChapterContent(siteSourceBean, chapterBos.get(1).getLink()))
////                .doOnSuccess(bo -> {System.out.println(bo);})
//                .subscribe(bean -> {
//                    System.out.println("WTF: " + bean);
//
//                }, e -> e.printStackTrace());


//        CollectionMediaBean collectionMediaBean = DaoDbHelper.getInstance().getSession().getShelfBookBeanDao().loadAll().get(0);
//        BookReadActivity.startActivity(MainActivity.this, collectionMediaBean, false);

//        CollectionMediaBean collectionMediaBean = new CollectionMediaBean();
//        WebMediaManager.getInstance().getSearchInfo("遮天")
//                .compose(RxUtils::toSimpleSingle)
//                .flatMap(resultBean -> WebMediaManager.getInstance().getMediaInfo(resultBean.getSiteSourceBean(), resultBean.getResultBeanList().get(0).getDetailUrl()))
//                .subscribe(bean -> {
//                    collectionMediaBean.setAuthor(bean.getAuthor());
//                    collectionMediaBean.setCategory(bean.getCategory());
//                    collectionMediaBean.setCoverUrl(bean.getCoverUrl());
//                    collectionMediaBean.setDesc(bean.getDesc());
//                    collectionMediaBean.setLastChapter(bean.getLastChapter());
//                    collectionMediaBean.setLastUpdated(bean.getLastUpdated());
//                    collectionMediaBean.setState(bean.getState());
//                    collectionMediaBean.setTitle(bean.getTitle());
//                    collectionMediaBean.setWordCount(bean.getWordCount());
//                    collectionMediaBean.setId(bean.getDetailUrl());
//                    collectionMediaBean.setCatalogUrl(bean.getCatalogUrl());
//                    collectionMediaBean.setCatalogNextPageUrl(bean.getCatalogNextPageUrl());
//                    collectionMediaBean.setBookSourceUrl(siteSourceBean.getSiteHostUrl());
//                }, Throwable::printStackTrace);

    }

    private static void importTencent() {

        SiteSourceBean sourceBean = new SiteSourceBean();
        sourceBean.setCreator("tw");
        sourceBean.setSiteHostUrl("https://m.ac.qq.com");
        sourceBean.setSiteIconUrl("https://ac.gtimg.com/media/images/ac_logo.png");
        sourceBean.setSiteName("腾讯动漫");
        sourceBean.setSiteDesc("付费章节无法看;速度快");
        sourceBean.setSiteType(MediaType.MEDIA_TYPE_COMIC);

        SiteSourceSearchBean sourceSearchBean = new SiteSourceSearchBean();
        SiteSourceSearchResultBean sourceSearchResultBean = new SiteSourceSearchResultBean();
        sourceSearchResultBean.setDetailUrl("");
        sourceSearchResultBean.setList("#lst_searchResult > li");
        sourceSearchResultBean.setDetailUrl("a @href");
        sourceSearchResultBean.setAuthor("");
        sourceSearchResultBean.setTitle("a > div.comic-content > strong @text");
        sourceSearchResultBean.setCoverUrl("a > div.comic-cover > img @src");
        sourceSearchResultBean.setLastUpdated("a > div.comic-content > small.comic-update @text");
        sourceSearchResultBean.setDesc("a > div.comic-content > small.comic-desc @text");
        sourceSearchBean.setResult(sourceSearchResultBean);
        sourceSearchBean.setUrl("/search/result");
        sourceSearchBean.setCharset("utf-8");
        Map<String, String> searchParams  = new HashMap<>();
        searchParams.put("word", "searchKey");
        sourceSearchBean.setParams(searchParams);
//        sourceSearchBean.setHeaders(Collections.emptyMap());
        sourceBean.setSearch(sourceSearchBean);


        SiteSourceMediaInfoBean bookBean = new SiteSourceMediaInfoBean();
        bookBean.setTitle("body > article > div.lay-head > section.mod-head > div.head-info > div.head-info-detail > ul > li.head-info-title > h1 @text");
        bookBean.setAuthor("body > article > div.lay-head > section.mod-head > div.head-info > div.head-info-detail > ul > li.head-info-author @text");
        bookBean.setDesc("body > article > div:nth-child(2) > section.mod-detail > div > p @text");
        bookBean.setLastChapter("");
        bookBean.setCoverUrl("body > article > div.lay-head > section.mod-head > div.head-info > div.head-info-cover > img @src");
        bookBean.setCatalogUrl("#btn_expandChapterList @href");
        sourceBean.setMediaInfoDetailRule(bookBean);

        SiteSourceChapterInfoBean chapterBean = new SiteSourceChapterInfoBean();
        chapterBean.setList("body > section.chapter-list-box.list-expanded > ul.chapter-list.normal > li");
        chapterBean.setUrl("a @href");
        chapterBean.setName("a @text");
        chapterBean.setOrderAsc(true);
        sourceBean.setChapterInfoRule(chapterBean);

        SiteSourceContentInfoBean contentBean = new SiteSourceContentInfoBean();
//        contentBean.setContentList("body > section.comic-pic-area.scroll-mode > section.comic-pic-list-all > ul > li");
        contentBean.setContent("@JS:dmFyIGRlY29kZT1mdW5jdGlvbihyYXcsX25vbmNlKXt2YXIgYT1TdHJpbmcuZnJvbUNoYXJDb2RlKCgxPDw3KS0zMSk7dmFyIGtleT1bImQiK2ErInQiK2EsIm1wIisibXZyIl0uam9pbigiLSIpO3ZhciBub25jZT1fbm9uY2V8fHdpbmRvdy5ub25jZTtyYXc9cmF3LnNwbGl0KCIiKTtub25jZT1ub25jZS5tYXRjaCgvXGQrW2EtekEtWl0rL2cpO3ZhciBsZW49bm9uY2UubGVuZ3RoO3doaWxlKGxlbi0tKXt2YXIgb2Zmc2V0PXBhcnNlSW50KG5vbmNlW2xlbl0pJjI1NTt2YXIgbm9pc2U9bm9uY2VbbGVuXS5yZXBsYWNlKC9cZCsvZywiIik7cmF3LnNwbGljZShvZmZzZXQsbm9pc2UubGVuZ3RoKX12YXIgX2RhdGE9QmFzZTY0LmRlY29kZShyYXcuam9pbigiIikpO3JldHVybiBKU09OLnBhcnNlKF9kYXRhKX07dmFyIHJlX2RhdGE9L2RhdGE6XHMqJyguKik/Lzt2YXIgcmVfbm9uY2U9LzxzY3JpcHQ+d2luZG93Lio/PSguKj8pPFwvc2NyaXB0Pi87dmFyIGRhdGE9cmVfZGF0YS5leGVjKCRjb250ZW50KVsxXTt2YXIgbm9uY2U9cmVfbm9uY2UuZXhlYygkY29udGVudClbMV07bm9uY2U9ZXZhbChub25jZSk7dmFyIHJldD1kZWNvZGUoZGF0YSxub25jZSk7dmFyICRyZXQ9W107Zm9yKGk9MDtpPHJldFsicGljdHVyZSJdLmxlbmd0aDtpKyspeyRyZXQucHVzaChyZXRbInBpY3R1cmUiXVtpXVsidXJsIl0pfTs=");
        sourceBean.setContentInfoRule(contentBean);


        DaoDbHelper.getInstance().getSession().insertOrReplace(sourceBean);
    }


    private static void importGuFengManHua() {

        SiteSourceBean sourceBean = new SiteSourceBean();
        sourceBean.setCreator("tw");
        sourceBean.setSiteHostUrl("https://m.gufengmh8.com");
        sourceBean.setSiteIconUrl("https://m.gufengmh8.com/themes/mip/phone/images/logo2.png");
        sourceBean.setSiteName("古风漫画");
        sourceBean.setSiteDesc("古风比较多;速度快");
        sourceBean.setSiteType(MediaType.MEDIA_TYPE_COMIC);

        SiteSourceSearchBean sourceSearchBean = new SiteSourceSearchBean();
        SiteSourceSearchResultBean sourceSearchResultBean = new SiteSourceSearchResultBean();
        sourceSearchResultBean.setDetailUrl("");
        sourceSearchResultBean.setList("#update_list > div > div");
        sourceSearchResultBean.setDetailUrl("div.itemImg > a @href");
        sourceSearchResultBean.setAuthor("div.itemTxt > p:nth-child(2) @text");
        sourceSearchResultBean.setTitle("div.itemTxt > a @text");
        sourceSearchResultBean.setCoverUrl("div.itemImg > a > mip-img @src");
        sourceSearchResultBean.setLastUpdated("div.itemTxt > p:nth-child(4) > span.date @text");
        sourceSearchResultBean.setCategory("div.itemTxt > p:nth-child(3) > span.pd @text");
//        sourceSearchResultBean.setDesc("a > div.comic-content > small.comic-desc @text");
        sourceSearchBean.setResult(sourceSearchResultBean);
        sourceSearchBean.setUrl("/search/");
        sourceSearchBean.setCharset("utf-8");
        Map<String, String> searchParams  = new HashMap<>();
        searchParams.put("keywords", "searchKey");
        sourceSearchBean.setParams(searchParams);
//        sourceSearchBean.setHeaders(new HashMap<>());
        sourceBean.setSearch(sourceSearchBean);

        SiteSourceMediaInfoBean bookBean = new SiteSourceMediaInfoBean();
        bookBean.setTitle("body > div.comic-view.clearfix > div.view-sub.autoHeight > h1 @text");
        bookBean.setAuthor("body > div.comic-view.clearfix > div.view-sub.autoHeight > div > dl:nth-child(3) > dd & text");
        bookBean.setCategory("body > div.comic-view.clearfix > div.view-sub.autoHeight > div > dl:nth-child(4) > dd @text");
        bookBean.setLastUpdated("body > div.comic-view.clearfix > div.view-sub.autoHeight > div > dl:nth-child(5) > dd @text");
        bookBean.setLastChapter("");
        bookBean.setCoverUrl("#Cover > mip-img @src");
//        bookBean.setCatalogUrl("#btn_expandChapterList @href");
        sourceBean.setMediaInfoDetailRule(bookBean);

        SiteSourceChapterInfoBean chapterBean = new SiteSourceChapterInfoBean();
        chapterBean.setList("#chapter-list-1 > li");
        chapterBean.setUrl("a @href");
        chapterBean.setName("a @text");
        chapterBean.setOrderAsc(true);
        sourceBean.setChapterInfoRule(chapterBean);

        SiteSourceContentInfoBean contentBean = new SiteSourceContentInfoBean();
//        contentBean.setContentList("body > section.comic-pic-area.scroll-mode > section.comic-pic-list-all > ul > li");
//        contentBean.setContent("@JS:var reg_imgs=/chapterImages = \[(.*?)\]/;var reg_path=/chapterPath = '(.*?)'/;imgs=reg_imgs.exec($content)[1].split(',');path=reg_path.exec($content)[1];var $ret=[];for(i in imgs){$ret.push('https://res.gufengmh8.com/'+path+imgs[i].slice(1,-1))};");
        contentBean.setContent("@JS:dmFyIHJlZ19pbWdzPS9jaGFwdGVySW1hZ2VzID0gXFsoLio/KVxdLzt2YXIgcmVnX3BhdGg9L2NoYXB0ZXJQYXRoID0gIiguKj8pIi87aW1ncz1yZWdfaW1ncy5leGVjKCRjb250ZW50KVsxXS5zcGxpdCgiLCIpO3BhdGg9cmVnX3BhdGguZXhlYygkY29udGVudClbMV07dmFyICRyZXQ9W107Zm9yKGkgaW4gaW1ncyl7JHJldC5wdXNoKCJodHRwczovL3Jlcy5ndWZlbmdtaDguY29tLyIrcGF0aCtpbWdzW2ldLnNsaWNlKDEsLTEpKX07");
        sourceBean.setContentInfoRule(contentBean);


        DaoDbHelper.getInstance().getSession().insertOrReplace(sourceBean);

    }

    private static void importChuiXue() {

        SiteSourceBean sourceBean = new SiteSourceBean();
        sourceBean.setCreator("tw");
        sourceBean.setSiteHostUrl("http://www.chuixue.net/");
        sourceBean.setSiteIconUrl("http://www.chuixue.net/skin/dh/logo.png");
        sourceBean.setSiteName("吹雪漫画");
        sourceBean.setSiteDesc("慢速;凑合看");
        sourceBean.setSiteType(1);

        SiteSourceSearchBean sourceSearchBean = new SiteSourceSearchBean();
        SiteSourceSearchResultBean sourceSearchResultBean = new SiteSourceSearchResultBean();
        sourceSearchResultBean.setList("#dmList > ul > li");
        sourceSearchResultBean.setDetailUrl("p > a @href");
        sourceSearchResultBean.setAuthor("div.itemTxt > p:nth-child(2) @text");
        sourceSearchResultBean.setTitle("dl > dt > a @text");
//        sourceSearchResultBean.setCoverUrl("p > a > img @src");
        sourceSearchResultBean.setCoverUrl("p > a > img @_src");
        sourceSearchResultBean.setLastUpdated("dl > dd > p:nth-child(1) > span @text");
        sourceSearchResultBean.setCategory("dl > dd > p:nth-child(3) @text");
        sourceSearchResultBean.setDesc("dl > dd > p.intro @text");
        sourceSearchBean.setResult(sourceSearchResultBean);
        sourceSearchBean.setUrl("/e/search/");
        sourceSearchBean.setCharset("gb2312");
        Map<String, String> searchParams  = new HashMap<>();
        searchParams.put("keyboard", "searchKey");
        searchParams.put("searchget", "1");
        searchParams.put("show", "title,player,playadmin,pinyin");
        sourceSearchBean.setParams(searchParams);
//        sourceSearchBean.setHeaders(Collections.emptyMap());
        sourceBean.setSearch(sourceSearchBean);

        SiteSourceMediaInfoBean bookBean = new SiteSourceMediaInfoBean();
        bookBean.setTitle("#intro_l > div.title > h1 @text");
        bookBean.setAuthor("");
        bookBean.setCategory("#intro_l > div.info > p:nth-child(5) > a @text");
        bookBean.setLastUpdated("#intro_l > div.info > p:nth-child(1) > span @text");
        bookBean.setLastChapter("");
        bookBean.setCoverUrl("#intro_l > div.info_cover > p > img @src");
        bookBean.setState("#intro_l > div.info > p:nth-child(3) @text");
//        bookBean.setCatalogUrl("#btn_expandChapterList @href");
        sourceBean.setMediaInfoDetailRule(bookBean);

        SiteSourceChapterInfoBean chapterBean = new SiteSourceChapterInfoBean();
        chapterBean.setList("#play_0 > ul > li");
        chapterBean.setUrl("a @href");
        chapterBean.setName("a @text");
        chapterBean.setOrderAsc(false);
        sourceBean.setChapterInfoRule(chapterBean);

        SiteSourceContentInfoBean contentBean = new SiteSourceContentInfoBean();
//        contentBean.setContentList("body > section.comic-pic-area.scroll-mode > section.comic-pic-list-all > ul > li");
        contentBean.setContent("@JS:dmFyIGpzX2V4cHI9LyhmdW5jdGlvblxzKmJhc2U2NFtcU1xzXSo/KTxcL3NjcmlwdD4vO3ZhciBqcz1qc19leHByLmV4ZWMoJGNvbnRlbnQpWzFdO2V2YWwoanMpO3ZhciBzZXJ2ZXI9Imh0dHA6Ly9jaHVpeHVlIjsrKHZpZXdpZD49NTE5MjUzPzE6MikrIi50aWFuc2hpZ2VnZS5jb20vIjt2YXIgJHJldD1bXTtmb3IoaSBpbiBwaG90b3NyKXskcmV0LnB1c2goc2VydmVyK3Bob3Rvc3JbaV0pfTs=");
//        contentBean.setPageTotal("#selectpage1 > select > option:last-child @value");
//        contentBean.setPageLinkList("$JS.ZnVuY3Rpb24gZ2V0UmVzdWx0KGQsYyxiKXt2YXIgYT1bXTtmb3IoaT0xO2k8PWI7aSsrKXthLnB1c2goZCsiP3BhZ2U9IitpKX1yZXR1cm4gYX07");
        sourceBean.setContentInfoRule(contentBean);


        DaoDbHelper.getInstance().getSession().insertOrReplace(sourceBean);

    }

    private static void import2Animx() {

        SiteSourceBean sourceBean = new SiteSourceBean();
        sourceBean.setCreator("tw");
        sourceBean.setSiteHostUrl("http://www.2animx.com");
        sourceBean.setSiteIconUrl("http://www.2animx.com/static/images/ly_logo.png");
        sourceBean.setSiteName("二次元动漫");
        sourceBean.setSiteDesc("速度一般");
        sourceBean.setSiteType(MediaType.MEDIA_TYPE_COMIC);

        SiteSourceSearchBean sourceSearchBean = new SiteSourceSearchBean();
        SiteSourceSearchResultBean sourceSearchResultBean = new SiteSourceSearchResultBean();
        sourceSearchResultBean.setList("body > div.box.across.list > div.box-bd > ul > li");
        sourceSearchResultBean.setDetailUrl("a @href");
        sourceSearchResultBean.setAuthor("");
        sourceSearchResultBean.setTitle("a > div.tit @text");
        sourceSearchResultBean.setCoverUrl("a > img @src");
        sourceSearchResultBean.setLastUpdated("a > font @text");
        sourceSearchResultBean.setLastChapter("a > em @text");
//        sourceSearchResultBean.setCategory("dl > dd > p:nth-child(3) @text");
//        sourceSearchResultBean.setDesc("dl > dd > p.intro @text");
        sourceSearchBean.setResult(sourceSearchResultBean);
        sourceSearchBean.setUrl("/search-index");
        sourceSearchBean.setCharset("utf-8");
        Map<String, String> searchParams  = new HashMap<>();
        searchParams.put("q", "searchKey");
        searchParams.put("searchType", "1");
        sourceSearchBean.setParams(searchParams);
//        sourceSearchBean.setHeaders(Collections.emptyMap());
        sourceBean.setSearch(sourceSearchBean);

        SiteSourceMediaInfoBean bookBean = new SiteSourceMediaInfoBean();
        bookBean.setTitle("body > div.main > div.position > strong @text");
        bookBean.setAuthor("body > div.main > div.fl.show-center > div.twobleft.center_nr > div > div:nth-child(1) > dl > dd > p:nth-child(1) > span @text");
        bookBean.setCategory("#classdh @text");
        bookBean.setLastUpdated("#intro_l > div.info > p:nth-child(1) > span @text");
//        bookBean.setLastChapter("body > div.main > div.fl.show-center > div.twobleft.center_nr > div > div:nth-child(1) > dl > dd > p:nth-child(5) > span:nth-child(2) @text");
        bookBean.setLastChapter("");
        bookBean.setCoverUrl("dl.mh-detail > dt > a > img @src");
        bookBean.setState("body > div.main > div.fl.show-center > div.twobleft.center_nr > div > div:nth-child(1) > dl > dd > p:nth-child(2) > a @text");
        bookBean.setDesc("body > div.main > div.fl.show-center > div.twobleft.center_nr > div > div.mh-introduce > p:nth-child(3) @text");
//        bookBean.setCatalogUrl("#btn_expandChapterList @href");
        sourceBean.setMediaInfoDetailRule(bookBean);

        SiteSourceChapterInfoBean chapterBean = new SiteSourceChapterInfoBean();
        chapterBean.setList("#oneCon2 > ul > li");
        chapterBean.setUrl("a @href");
        chapterBean.setName("a @text");
        chapterBean.setOrderAsc(false);
        sourceBean.setChapterInfoRule(chapterBean);

        SiteSourceContentInfoBean contentBean = new SiteSourceContentInfoBean();
//        contentBean.setContentList("body > section.comic-pic-area.scroll-mode > section.comic-pic-list-all > ul > li");
        contentBean.setContent("#ComicPic @src");
        contentBean.setPageTotal("#total @value");
//        contentBean.setPageLinkList("@JS:ZnVuY3Rpb24gZ2V0UmVzdWx0KGQsYyxiKXt2YXIgYT1bXTtmb3IoaT0xO2k8PWI7aSsrKXthLnB1c2goZCsiLXAtIitpKX1yZXR1cm4gYX07");
        contentBean.setPageLinkList("@JS:var $ret=[];for(i=1;i<=$pTotal;i++){$ret.push($url+'-p-'+i)};");
        sourceBean.setContentInfoRule(contentBean);


//        HashMap<String, String> categoryMap = new HashMap<>();
//        categoryMap.put("科幻魔幻", "http://www.2animx.com/index-html-typeid-1");
//        categoryMap.put("少年热血", "http://www.2animx.com/index-html-status-0-typeid-2-sort-");
//        categoryMap.put("少女爱情", "http://www.2animx.com/index-html-status-0-typeid-4-sort-");
//        categoryMap.put("武侠格斗", "http://www.2animx.com/index-html-status-0-typeid-5-sort-");
//        categoryMap.put("爆笑喜剧", "http://www.2animx.com/index-html-status-0-typeid-6-sort-");
//        categoryMap.put("竞技体育", "http://www.2animx.com/index-html-status-0-typeid-8-sort-");
//        categoryMap.put("侦探推理", "http://www.2animx.com/index-html-status-0-typeid-9-sort-");
//        categoryMap.put("恐怖灵异", "http://www.2animx.com/index-html-status-0-typeid-10-sort-");
//        categoryMap.put("BL", "http://www.2animx.com/index-html-status-0-typeid-11-sort-");
//        categoryMap.put("恋爱", "http://www.2animx.com/index-html-status-0-typeid-15-sort-");
//        categoryMap.put("治愈", "http://www.2animx.com/index-html-status-0-typeid-16-sort-");
//        categoryMap.put("校园", "http://www.2animx.com/index-html-status-0-typeid-19-sort-");
//        categoryMap.put("耽美", "http://www.2animx.com/index-html-status-0-typeid-20-sort-");
//        categoryMap.put("百合", "http://www.2animx.com/index-html-status-0-typeid-22-sort-");
//        categoryMap.put("冒险", "http://www.2animx.com/index-html-status-0-typeid-25-sort-");
//        categoryMap.put("后宫", "http://www.2animx.com/index-html-status-0-typeid-26-sort-");
//        categoryMap.put("科幻", "http://www.2animx.com/index-html-status-0-typeid-27-sort-");
//        categoryMap.put("同人", "http://www.2animx.com/index-html-status-0-typeid-28-sort-");
//        categoryMap.put("奇幻", "http://www.2animx.com/index-html-status-0-typeid-29-sort-");
//        categoryMap.put("魔法", "http://www.2animx.com/index-html-status-0-typeid-32-sort-");
//        categoryMap.put("历史", "http://www.2animx.com/index-html-status-0-typeid-33-sort-");
//        categoryMap.put("战争", "http://www.2animx.com/index-html-status-0-typeid-34-sort-");
//        categoryMap.put("神鬼", "http://www.2animx.com/index-html-status-0-typeid-36-sort-");
////        sourceBean.setMediaCategory(categoryMap);
//        SiteSourceCategoryInfoBean categoryInfoBean = new SiteSourceCategoryInfoBean();
//        //TODO div.box.across.list这种多个class限定一个tag的问题？
////        categoryInfoBean.setList("#body > div.box.across.list > div.box-bd > ul > li");
//
//        categoryInfoBean.setList("ul.liemh > li");
//        categoryInfoBean.setNextPage("body > div.box.across.list > div.box-bd > div.pagination.pagination-normal.mt20 > div > a.next:contains(下一頁) @ href");
//        categoryInfoBean.setDetailUrl("a @href");
//        categoryInfoBean.setTitle("a > div.tit@text");
//        categoryInfoBean.setLastChapter("a > em @text");
//        categoryInfoBean.setCoverUrl("a > img@src");
//        categoryInfoBean.setLastUpdated("a > font@text ||| @JS:$content.substring($content.indexOf('更新時間：') + 5)");
////        sourceBean.setMediaCategoryRule(categoryInfoBean);

        DaoDbHelper.getInstance().getSession().insertOrReplace(sourceBean);

    }


    private void testJsoup(String body) {
        Document doc = Jsoup.parse(body);
//        Elements elements = doc.select(" #main > div > li > span.s2 > a @text");
        Elements elements = doc.select(" .novelslistss > li");

        for (Element element : elements) {

            System.out.println("li 内容: " + element);
            //nth-of-type(n)这里的n是从1开始，不是0开始
            element = element.select("a:nth-of-type(1)").get(0);
            System.out.println("检索结果：" + element.text() + " : " + element.attr("href"));
        }
    }
}
